<template>
  <div class="table">
    <div class="container">
      <div class="handle-box">
        <el-form ref="queryFormData" :model="queryFormData" inline size="small">
          <el-form-item label="歌名" style="margin-left: 10px">
            <el-input v-model="queryFormData.songName" clearable/>
          </el-form-item>
          <el-form-item label="歌手">
            <el-input v-model="queryFormData.singerName" clearable/>
          </el-form-item>
          <el-form-item label="专辑">
            <el-input v-model="queryFormData.songAlbumName" clearable/>
          </el-form-item>
          <el-form-item label="有效性">
            <el-select v-model="queryFormData.dataFlag" style="width: 150px">
              <el-option :value="0" label="有效"/>
              <el-option :value="1" label="无效"/>
            </el-select>
          </el-form-item>
          <el-button size="small" icon="el-icon-search" @click="search">搜索</el-button>
        </el-form>
      </div>
      <el-table v-loading="loading" :data="tableData" size="mini" style="width: 100%">
        <el-table-column label="歌曲图片" width="110" align="center">
          <template slot-scope="{row}">
            <div class="song-img">
              <img :src="row.pic" style="width: 100%;"/>
            </div>
            <div class="play" v-if="row.url !== ''"
                 @click="setSongUrl(row.url, row.name)">
              <div v-if="row.name === toggle">
                <svg class="icon">
                  <use xlink:href="#icon-zanting"/>
                </svg>
              </div>
              <div v-if="row.name !== toggle">
                <svg class="icon">
                  <use xlink:href="#icon-bofanganniu"/>
                </svg>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="歌名" width="170" align="center"/>
        <el-table-column prop="singerName" label="歌手" width="120" align="center"/>
        <el-table-column prop="albumName" label="专辑" width="120" align="center"/>
        <el-table-column prop="stylesStr" label="流派" width="110" align="center"/>
        <el-table-column prop="lyric" label="歌词" align="center">
          <template slot-scope="{row}">
            <p style="height: 100px; overflow: scroll">{{ row.lyric }}</p>
          </template>
        </el-table-column>
        <el-table-column prop="collectTimeStr" label="收藏时间" width="90" align="center"/>
        <el-table-column align="center" label="评论" prop="comment" width="100">
          <template slot-scope="{row}">
            <el-button size="mini" :disabled="row.collectDataFlag === 1"
                       @click="comment(row.id)" icon="el-icon-edit">评论
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="170">
          <template slot-scope="{ row }">
            <el-switch v-model="row.collectDataFlag" :active-value="0"
                       :inactive-value="1" active-text="生效"
                       inactive-text="失效" @change="change(row.id)"/>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination @current-change="handleCurrentChange" background
                       layout="prev, pager, next, total"
                       :current-page="pageNum" :page-size="pageSize" :total="total"/>
      </div>
    </div>
  </div>
</template>

<script>
import {mapGetters} from 'vuex';
import '@/assets/js/iconfont.js';
import {mixin} from '../mixins';
import {loveSong, updateLoveSongDataFlag} from '../api/index';

export default {
  mixins: [mixin],
  data() {
    return {
      queryFormData: {
        userId: this.$route.query.userId,
        songName: '',
        singerName: '',
        songAlbumName: '',
        dataFlag: 0
      },
      tableData: [],
      pageNum: 1,
      pageSize: 10,
      total: 0,
      loading: true,
      toggle: false, // 控制播放图标状态
    }
  },
  computed: {
    ...mapGetters([
      'isPlay'
    ])
  },
  mounted() {
    this.getData();
  },
  destroyed() {
    // 离开当前页面暂停播放
    this.$store.commit('setIsPlay', false);
  },
  methods: {
    getData() {
      this.loading = true;
      const {queryFormData, pageNum, pageSize} = this;
      loveSong(queryFormData, pageNum, pageSize).then(res => {
        this.loading = false;
        const {data, total, success} = res;
        if (!success) {
          this.$message.error('查询失败');
          this.tableData = [];
          return;
        }
        this.tableData = data.map(e => {
          return {
            ...e,
            toggle: false
          }
        });
        this.total = total;
      })
    },
    handleCurrentChange(currentPageNum) {
      this.pageNum = currentPageNum;
      this.getData();
    },
    search() {
      this.getData();
    },
    setSongUrl(url, name) {
      this.toggle = name;
      this.$store.commit('setUrl', url);
      if (this.isPlay) {
        this.$store.commit('setIsPlay', false);
      } else {
        this.$store.commit('setIsPlay', true);
      }
    },
    comment(songId) {
      this.$router.push({path: "/Comment", query: {songId: songId}});
    },
    change(id) {
      let data = {
        songId: id,
        userId: this.$route.query.userId
      };
      updateLoveSongDataFlag(data).then(res => {
        const {success} = res;
        if (success) {
          this.$message.success("操作成功");
          this.getData();
        } else {
          this.$message.error("操作失败");
        }
      });
    }
  }
}

</script>

<style scoped>
.song-img {
  width: 100%;
  height: 90px;
  margin-top: 5px;
  margin-bottom: 5px;
  overflow: hidden;
}

.play {
  position: absolute;
  z-index: 100;
  width: 80px;
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  top: 18px;
  left: 15px;
}

.icon {
  width: 2em;
  height: 2em;
  color: white;
  fill: currentColor;
  overflow: hidden;
}
</style>